import * as Echarts from 'echarts/core';

export function getEchartsLinearColors(dir, colors) {
  const dirMap = {
    t2b: [0, 0, 0, 1],
    b2t: [0, 1, 0, 0],
    l2r: [0, 0, 1, 0],
    r2l: [1, 0, 0, 0],
  };

  if (typeof arguments[0] !== 'string') {
    dir = arguments[1];
    colors = arguments[0];
  }
  dir = dirMap[dir] || dirMap.l2r;
  if (!colors || !Array.isArray(colors)) {
    colors = [
      { offset: 0, color: 'rgba(35,157,250,1)' },
      { offset: 1, color: 'rgba(35,157,250,0)' },
    ];
  }
  colors = colors.map((item, index) => {
    return typeof item === 'string' ? { offset: index, color: item } : item;
  });
  /* right,bottom.left,top,colors */
  return new Echarts.graphic.LinearGradient(...dir, colors);
}
// 饼图中心图标位置定位
export function getPieCenterIconStyle(center) {
  return {
    position: 'absolute',
    transform: 'translate(-50%, -50%)',
    left: center[0] || '50%',
    top: center[1] || '50%',
  };
}
